<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="1000px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="wrapper">
      <SplitForm ref="splitForm" :operateType="operateType" :form="form" :dicts="allDicts" />
      <TimeLine :id="form.id" v-if="operateType != 'add' && operateType != 'sync' && form.id" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit" v-if="operateType != 'view'">确定</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRevisePeople, getDutyArea } from "@/api/biz/ledger/ledger";
import { getDataList } from "../../qualityFile/api";
import { getTree } from "@/api/biz/enter/enterNext";
import { addData, updateData, getQuestionList } from "../api";
import { listType } from "@/api/list/type";
import SplitForm from "./splitForm.vue";
import TimeLine from "./timeLine.vue"
export default {
  name: "AddDialog",
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "质量问题文件详情"
    },
    data: {
      type: Object,
      default: () => { }
    },
    operateType: {
      type: String,
      default: "view"
    },
    dicts: {
      type: Object,
      default: () => { }
    }
  },
  components: { SplitForm, TimeLine },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      fileList: [],
      form: {
        id: "",
        fileId: "", // 质量问题ID
        workNo: "", // 问题编号
        splitTime: "", // 拆分时间
        questionDesc: "", // 问题描述
        supplement: "", // 补充说明
        dutyAreaId: null, // 责任区域
        dutyAreaName: "", // 责任区域
        controlMeasures: "", // 防治措施
        questionLevel: '', // 问题等级
        reworkOrNot: '', // 是否返工
        publishOrNot: '', // 是否公开
        rectificationDeadlineStatus: '', // 整改期限
        questionBeyond: '', // 问题类型
        questionAnalysis: '', // 问题分析
        questionId: null, // 质量问题分类
        questionIdName: '', // 质量问题分类
        reviseTheDates: '', // 整改截止时间
        dataSource: '', // 数据来源
        checkType: null, // 检查类型
        checkPeopleId: '', // 检查人
        revisePeopleId: '', // 整改人
        reviewUserNameId: '', // 复查人
        notifierId: '', // 通知人
        fileIds: [], // 图片
        attachList: [] // 图片集合
        // canDel: "1", // 1可删除，2不可删除
      },
      fileOptions: [],
      dutyAreaOptions: [],
      checkTypeOptions: [],
      questionOptions: [],
      peopleOptions: [],
      libOptions: [], // 质量问题库
    };
  },
  created() {
    this.initData();
  },
  computed: {
    allDicts() {
      return {
        ...this.dicts,
        fileOptions: this.fileOptions,
        dutyAreaOptions: this.dutyAreaOptions,
        questionOptions: this.questionOptions,
        peopleOptions: this.peopleOptions,
        checkTypeOptions: this.checkTypeOptions,
        libOptions: this.libOptions,
      }
    }
  },
  methods: {
    //格式化数据
    // formatTreeData(data) {
    //   data.forEach((item) => {
    //     if (item.children && item.children.length) {
    //       this.formatTreeData(item.children)
    //     } else if (item.questionChildren && item.questionChildren.length) {
    //       item.questionChildren.forEach((subItem) => {
    //         subItem.label = subItem.questionInfo;
    //       });
    //       item.children = item.questionChildren
    //     }
    //   });
    // },
    initData() {
      // 质量问题库
      // if (this.operateType == 'add' || this.operateType == 'edit') {
        getQuestionList().then((res) => {
          this.libOptions = res.data;
          // this.formatTreeData(this.libOptions);
        });
      // }
      // 质量问题
      getDataList({ projectId: this.projectId, pageSize: 100000 }).then((data) => {
        this.fileOptions = data.rows.map(item => ({ label: item.fileName, value: item.id }))
      })
      // 整改人，复查人，通知人, 检查人
      getRevisePeople({ projectId: this.projectId, hiddenTrouble: '0' }).then((data) => {
        const res = data.rows.map(item => ({ label: item.nickName, value: item.userId.toString() }))
        this.peopleOptions = res;
      });
      // 责任区域
      getDutyArea({ projectId: this.projectId, hiddenTrouble: '0' }).then((response) => {
        this.dutyAreaOptions = response.data;
      });
      // 检查类型
      getTree({ checkTypeNo: null }).then((response) => {
        this.checkTypeOptions = response.data
      });
      // 质量问题分类
      listType().then((response) => {
        this.questionOptions = this.handleTree(response.data, "typeNo", "parentId");
      });
      if (this.operateType == 'add') {
        this.form.splitTime = this.parseTime(new Date(), '{y}-{m}-{d}');
        let num = '';
        for (var i = 0; i < 3; i++) {
          num += Math.floor(Math.random() * 10);
        }
        this.form.workNo = "zlwtk_" + this.parseTime(new Date(), '{y}{m}{d}{h}{i}{s}') + num;
      }
      if (this.operateType == 'edit' || this.operateType == 'view') {
        Object.keys(this.data).forEach(key => {
          this.$set(this.form, key, this.data[key])
        })
      }
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      const res1 = await this.$refs.splitForm.validate()
      if (!res1) {
        return;
      }
      const fn = this.form.id ? updateData : addData;
      const urlList = []
      this.form.attachList.forEach(e => {
        urlList.push(e.fileId)
      })
      this.form.fileIds = urlList.join(',')
      const res = await fn({ ...this.form, projectId: this.projectId })
      if (res.code == 200) {
        this.$emit("refresh")
        this.beforeClose()
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
